/*
Written by Best Flash Solutions, Inc  
See:
	http://www.bestflashsolutions.com
Contact me: contact@bestflashsolutions.com
*/

/**
 * Window for Video Display
 */
package com.bestflashsolutions.red5Streaming
{
	import com.bestflashsolutions.CommonUtils;
	import com.bestflashsolutions.components.ChatWindowLayoutCanvas;
	import com.bestflashsolutions.components.CnvMessage;
	import com.bestflashsolutions.components.MyPanel.DragPanel;
	import com.bestflashsolutions.view.DJSingleChat;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.VideoDisplay;
	import mx.core.Application;
	
	public class PlayLive extends DragPanel
	{
		//Show video
		public var objVideoDsp: VideoDisplay;
		//Get stream
		public var nsSubscribes: NetStream;
		public var netStatus: Function;
		private var strStreamName: String;
		public var isPlaying: Boolean = false;
		private var isAvailable: Boolean = false;
		//video size
		public var cam_width: Number = 320;
		public var cam_height: Number = 240;
		private var videoRecord: Video; 
		private var chatDropDown:Button;
		private var djsngleChat:DJSingleChat
		private var cnvMessage:CnvMessage;

		private var spaceGap = 40;
		private var ncCon:NetConnection;
		public var spaceHorGap:int = 10;
		public var spaceVerGap:int = 30;

		private var oldStreamGuid:String;
		
		public var chatBoxOn:Boolean;
		public var popedOut:Boolean;
		
		public function PlayLive(nc: NetConnection)
		{

			ncCon = nc;

			width = cam_width;
			height = cam_height;

			objVideoDsp = new VideoDisplay();

			objVideoDsp.maintainAspectRatio = false;
			addChild(objVideoDsp);
			objVideoDsp.x = 0;
			objVideoDsp.y = 0;
			objVideoDsp.setStyle("top",2)
			objVideoDsp.width = width;
			objVideoDsp.height = height ;
			nsSubscribes = new NetStream(nc);
			//Prevent show scrollbar
			horizontalScrollPolicy = "off";
			verticalScrollPolicy = "off";	
			objVideoDsp.doubleClickEnabled = true;
			objVideoDsp.addEventListener(MouseEvent.DOUBLE_CLICK,handlePopout);
		}
		
		//Start play (get streaming) for specific user
		public function play() {
			//Check if stream name is available
			if (!isAvailable) {
				netStatus(null);
				return;
			}
			if(!nsSubscribes)
				nsSubscribes = new NetStream(ncCon);

			oldStreamGuid = _streamGuid;
			videoRecord = new Video();
      	    //videoRecord.height = cam_height;
    	    //videoRecord.width = cam_width;
        	nsSubscribes.addEventListener(NetStatusEvent.NET_STATUS , netStatus );	
        	//nsSubscribes.client = new EventManager(this);
        	videoRecord.attachNetStream(nsSubscribes);
     		
     		//objVideoDsp.addEventListener(Event.RESIZE, resizeHandler);
        	objVideoDsp.addChild(videoRecord);
        	//receive streaming
        	nsSubscribes.play(streamGuid, -1);
        	//resize video to fit window
        	mouseUpHandler(null);
		}
		
		private function updatePlay():void{
			oldStreamGuid = _streamGuid;
			nsSubscribes.removeEventListener(NetStatusEvent.NET_STATUS , netStatus );	
			nsSubscribes.close();
			if(videoRecord)
				objVideoDsp.removeChild(videoRecord);
			videoRecord = null;
			nsSubscribes = null;
			nsSubscribes = new NetStream(ncCon);
			play();		
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{	
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			if(chatDropDown)
				chatDropDown.y = Math.round(objVideoDsp.y + objVideoDsp.height - chatDropDown.height);
			if(djsngleChat)
				djsngleChat.y = Math.round(objVideoDsp.y + objVideoDsp.height);
			if(cnvMessage){
				cnvMessage.x = width - cnvMessage.width - 10;
				cnvMessage.y = Math.round(objVideoDsp.y + objVideoDsp.height - cnvMessage.height);
			}
		}
		
		public function addChatControls(argRoomName:String,argJoiner:String):void{
			if(!chatDropDown){
				chatDropDown = new Button();
				chatDropDown.name = "chatDownButton";
				chatDropDown.styleName = "chatDropDownBtn";
				chatDropDown.setStyle("left",5);
				chatDropDown.toggle = true;
				chatDropDown.visible = false;
				chatDropDown.addEventListener(MouseEvent.CLICK,dropDownHanlder);
				addChild(chatDropDown);
			}else{
				if(!getChildByName("chatDownButton"))
					addChild(chatDropDown);
			}
			//if(!djsngleChat){
				djsngleChat = new DJSingleChat();
				djsngleChat.name = "singleChat";
				djsngleChat.percentWidth = 100;
				djsngleChat.connectToServerHandler(argRoomName);
				djsngleChat.ownerName = argJoiner;
				djsngleChat.addEventListener("messageReceived",newMessageHandler);
				djsngleChat.addEventListener(MouseEvent.CLICK,messageReadHandler);
				addChild(djsngleChat);
			//}else{
			//	if(!getChildByName("singleChat"))
			//		addChild(djsngleChat);
			//}
			
			
			fitScaleCam();
		}
		
		private function newMessageHandler(evt:Event=null):void{
			if(cnvMessage==null){
				cnvMessage=new CnvMessage();
				cnvMessage.x = width - cnvMessage.width - 10;
				cnvMessage.y = Math.round(objVideoDsp.y + objVideoDsp.height - cnvMessage.height);
				addChild(cnvMessage)
			}	
		}
		
		private function messageReadHandler(evt:Event=null):void{
			if(cnvMessage){
				removeChild(cnvMessage)
				cnvMessage = null;
			}	
		}
		
		
		private function dropDownHanlder(evt:Event=null):void{
			if(chatDropDown.selected){
				djsngleChat.height = djsngleChat.desiredHeight;
				height = objVideoDsp.height + spaceVerGap +djsngleChat.height; 
				messageReadHandler();
				chatBoxOn = true;
			}else{
				djsngleChat.height = 0;
				height = objVideoDsp.height + spaceVerGap; 
				chatBoxOn = false;
			}
			Application.application.cvsRoom.updateLayOut(false);
			
		}
		
		private function handlePopout(evt:Event):void{
			Application.application.cvsRoom.updateLayOut(true,this);
		}
		
		public function showChatBox():void{
		    if(chatDropDown){
				chatDropDown.visible=true;
			}
		}
		
		public function restore():void{
			if(chatDropDown){
				chatDropDown.visible=false;
				chatDropDown.selected=false;
			}
			if(djsngleChat){
				djsngleChat.height = 0;
				height = objVideoDsp.height + spaceVerGap; 
				chatBoxOn = false;
			}
		}
		
		//When resize display window
		protected override function mouseUpHandler(event:MouseEvent):void {
			
			if (event && Application.application.cvsRoom.layout == ChatWindowLayoutCanvas.LAYOUT_FLOAT) 
				super.mouseUpHandler(event);
			
			resWidth = width;
			resHeight = height;
			//objVideoDsp.width = cam_width;
			//objVideoDsp.height = cam_height;
			/*CommonUtils.fitScale(objVideoDsp, new Rectangle(0, 0, renderWidth, renderHeight));
			CommonUtils.moveObjectCenterPoint(objVideoDsp, new Point(renderWidth/2, renderHeight/2));*/
			/*objVideoDsp.width = renderWidth;
			objVideoDsp.height = objVideoDsp.width/cam_width*cam_height;
			videoRecord.width = objVideoDsp.width;
			videoRecord.height = objVideoDsp.height;*/
			fitScaleCam()
			objVideoDsp.validateSize();
			
		}
		
		protected override function mouseUpMovePanelHandler(event:MouseEvent):void{
			resX = x;
			resY = y;
			super.mouseUpMovePanelHandler(event);
			
		}
		public function fitScaleCam():void {
			//objVideoDsp.width = cam_width;
			//objVideoDsp.height = cam_height;
			//CommonUtils.fitScale(objVideoDsp, new Rectangle(0, 0, renderWidth, renderHeight));
			//CommonUtils.moveObjectCenterPoint(objVideoDsp, new Point(renderWidth/2, renderHeight/2));
			if(objVideoDsp){
				objVideoDsp.width = renderWidth;
				objVideoDsp.height = objVideoDsp.width/cam_width*cam_height;
			}else{
				return;
			}
			if(videoRecord){
				videoRecord.width = objVideoDsp.width;
				videoRecord.height = objVideoDsp.height;
			}
			objVideoDsp.validateSize();
			
			if(djsngleChat)
				height = objVideoDsp.height+spaceVerGap+djsngleChat.height;
			else
				height = objVideoDsp.height+spaceVerGap;
			//trace("fitScaleCam->"+width+"x"+height)
		}
		
		//Stop receive streaming
		public function stop() {
			if (!isAvailable) return;
			if (nsSubscribes!=null) {
				nsSubscribes.removeEventListener(NetStatusEvent.NET_STATUS , netStatus );	
				nsSubscribes.close();
				if(videoRecord&&objVideoDsp.contains(videoRecord))
					objVideoDsp.removeChild(videoRecord);
				videoRecord = null;
				nsSubscribes = null;
			}
			//objVideoDsp.visible = false;
		}
		
		
		//Check if this is available to receive stream
		public function get available(): Boolean {
			return isAvailable;
		}
		
		public function set available(v: Boolean): void {
			isAvailable =    v;
			visible = isAvailable;
			isPlaying = false;	
		}
		
		//Streaming name = username
		public function get streamName(): String {
			return strStreamName;
		}
		public function set streamName(v: String): void {
			strStreamName = v;
			title = strStreamName;	
		}
		//---------------
		private var _permission:String;
		public function get permission(): String {
			return _permission;
		}
		public function set permission(v: String): void {
			_permission = v;	
		}
		//---------------
		private var _joinDate:Number;
		public function get joinDate(): Number {
			return _joinDate;
		}
		public function set joinDate(v: Number): void {
			_joinDate = v;	
		}
		//---------------
		private var _isNew:Boolean;
		public function get isNew(): Boolean {
			return _isNew;
		}
		public function set isNew(v: Boolean): void {
			_isNew = v;	
		}
		//---------------
		private var _streamGuid:String;
		//Streaming video guid name
		public function get streamGuid(): String {
			return _streamGuid;
		}
		public function set streamGuid(v: String): void {
			_streamGuid = v;
		}
		
		public function updateStreamGuid(vidGuidData:String):void{
			_streamGuid = vidGuidData;
			if(videoRecord && nsSubscribes && oldStreamGuid != _streamGuid && _streamGuid != ""){
				updatePlay();
			}else{
				if(isAvailable && videoRecord == null)
					updatePlay();
			}
		}
		
		//---------------
		private var _resWidth:int=cam_width;
		public function get resWidth(): int {
			return _resWidth;
		}
		public function set resWidth(v: int): void {
			_resWidth = v;	
		}
		//---------------
		private var _resHeight:int=cam_height;
		public function get resHeight(): int {
			return _resHeight;
		}
		public function set resHeight(v: int): void {
			_resHeight = v;	
		}
		//---------------
		private var _resX:int=0;
		public function get resX(): int {
			return _resX;
		}
		public function set resX(v: int): void {
			_resX = v;	
		}
		//---------------
		private var _resY:int=0;
		public function get resY(): int {
			return _resY;
		}
		public function set resY(v: int): void {
			_resY = v;	
		}
		//---------------
		
	}
}